package com.duowan.realtime.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.duowan.datawarehouse.utils.MapUtil;

public class MapUtils {
	public static Map convertToMap(Object[] array, String... keys) {
		if (array == null || array.length == 0) {
			return new HashMap();
		}
		Map map = new LinkedHashMap();
		for (int i = 0; i < keys.length; i++) {
			if (array.length == i) {
				break;
			}
			map.put(keys[i], array[i]);
		}
		return map;
	}
	
	public static String parseMapToString(Map map, String fieldSeperator, String mapKeySeperator){
		if (map == null || map.isEmpty()) {
			return null;
		}
		List<Object> kvList = new ArrayList<Object>();
		Set<Object> keySet = map.keySet();
		for (Object key : keySet) {
			kvList.add((String) key + mapKeySeperator + (String) map.get(key));
		}
		return StringUtils.join(kvList, fieldSeperator);
	}
	
	public static Map<String,String> parseStringToMap(String sourceStr, String fieldSeperator, String mapKeySeperator){
		if (StringUtils.isBlank(sourceStr)) {
			return null;
		}
		return MapUtil.parseMap(sourceStr, fieldSeperator, mapKeySeperator);
	}
}
